package com.seo4j.adb4j;

import com.esotericsoftware.kryo.serializers.DefaultSerializers;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

import java.io.IOException;

/**
 * Created by vps on 2016/9/13 0013.
 */
public class AdbServer {
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
        try {
            //      5554~5585
            server.bind(5037);
            server.getKryo().register(AdbMessage.class, new DefaultSerializers.ByteSerializer());

        } catch (IOException e) {
            e.printStackTrace();
        }
        server.addListener(new Listener() {
            @Override
            public void connected(Connection connection) {
                System.out.println("连接:" + connection.getID());
                AdbMessage message = new AdbMessage();
                //   message.set();
                connection.sendTCP(message);
            }

            @Override
            public void disconnected(Connection connection) {
                System.out.println("断开:" + connection.getID());
                super.disconnected(connection);
            }

            @Override
            public void idle(Connection connection) {
                System.out.println("空闲:" + connection.getID());
            }

            @Override
            public void received(Connection connection, Object object) {
                System.out.println("接受:" + connection.getID());

                if (object instanceof AdbMessage) {
//                    ByteBuffer request = (ByteBuffer) object;
//
//                    int length = request.getInt();
//                    byte[] buffer = new byte[length];
//                    request.get(buffer, 4, length);
//                    String command = new String(buffer, Charset.forName("utf-8"));
//                    System.out.println(command);
//                    SomeResponse response = new SomeResponse();
//                    response.text = "Thanks";
//                    connection.sendTCP(response);
                }
            }
        });
        //    System.out.println(String.format("%04x", 911l));
    }
}
